package com.example.software.controller;

import com.example.software.service.TaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/api/task-analysis")
public class TaskAnalysisController {
    
    private static final Logger log = LoggerFactory.getLogger(TaskAnalysisController.class);
    
    @Autowired
    private TaskService taskService;
    
    /**
     * 获取任务统计数据
     */
    @GetMapping("/statistics")
    public ResponseEntity<Map<String, Object>> getTaskStatistics(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.debug("获取任务统计数据 - 用户ID: {}", userId);
        
        try {
            Map<String, Object> statistics = taskService.getTaskStatistics(userId);
            return ResponseEntity.ok(statistics);
        } catch (Exception e) {
            log.error("获取任务统计数据时出错:", e);
            return ResponseEntity.status(500).build();
        }
    }
    
    /**
     * 获取任务状态分布
     */
    @GetMapping("/status-distribution")
    public ResponseEntity<Map<String, Integer>> getTaskStatusDistribution(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.debug("获取任务状态分布 - 用户ID: {}", userId);
        
        try {
            Map<String, Integer> distribution = taskService.getTaskStatusDistribution(userId);
            return ResponseEntity.ok(distribution);
        } catch (Exception e) {
            log.error("获取任务状态分布时出错:", e);
            return ResponseEntity.status(500).build();
        }
    }
    
    /**
     * 获取任务类型分布
     */
    @GetMapping("/type-distribution")
    public ResponseEntity<Map<String, Integer>> getTaskTypeDistribution(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.debug("获取任务类型分布 - 用户ID: {}", userId);
        
        try {
            Map<String, Integer> distribution = taskService.getTaskTypeDistribution(userId);
            return ResponseEntity.ok(distribution);
        } catch (Exception e) {
            log.error("获取任务类型分布时出错:", e);
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取任务薪资趋势
     */
    @GetMapping("/salary-trend")
    public ResponseEntity<List<Map<String, Object>>> getTaskSalaryTrend(
            @RequestParam(defaultValue = "30days") String period,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.debug("获取任务薪资趋势 - 用户ID: {}, 周期: {}", userId, period);
        
        try {
            List<Map<String, Object>> trend = taskService.getTaskSalaryTrend(userId, period);
            return ResponseEntity.ok(trend);
        } catch (Exception e) {
            log.error("获取任务薪资趋势时出错:", e);
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取任务完成率统计
     */
    @GetMapping("/completion-stats")
    public ResponseEntity<List<Map<String, Object>>> getTaskCompletionStats(
            @RequestParam(defaultValue = "7days") String range,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.debug("获取任务完成率统计 - 用户ID: {}, 范围: {}", userId, range);
        
        try {
            List<Map<String, Object>> stats = taskService.getTaskCompletionStats(userId, range);
            return ResponseEntity.ok(stats);
        } catch (Exception e) {
            log.error("获取任务完成率统计时出错:", e);
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取任务地区分布
     */
    @GetMapping("/location-distribution")
    public ResponseEntity<Map<String, Integer>> getTaskLocationDistribution(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.debug("获取任务地区分布 - 用户ID: {}", userId);
        
        try {
            Map<String, Integer> distribution = taskService.getTaskLocationDistribution(userId);
            return ResponseEntity.ok(distribution);
        } catch (Exception e) {
            log.error("获取任务地区分布时出错:", e);
            return ResponseEntity.status(500).build();
        }
    }
} 